package com.irdstudio.efp.cus.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.cus.service.dao.CompanyWhiteListDao;
import com.irdstudio.efp.cus.service.domain.CompanyWhiteList;
import com.irdstudio.efp.cus.service.domain.PersonalWhiteList;
import com.irdstudio.efp.cus.service.facade.CompanyWhiteListService;
import com.irdstudio.efp.cus.service.vo.CompanyWhiteDeleteExcelVO;
import com.irdstudio.efp.cus.service.vo.CompanyWhiteListVO;
import com.irdstudio.efp.cus.service.vo.PersonalWhiteListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.List;
import java.util.Objects;

/**
 * Service Impl: 企业白名单服务实现
 *
 * @author gcb
 * @date 2020-07-31
 */
@Service("companyWhiteListService")
public class CompanyWhiteListServiceImpl implements CompanyWhiteListService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(CompanyWhiteListServiceImpl.class);

    @Autowired
    private CompanyWhiteListDao companyWhiteListDao;

    /**
     * 新增数据
     *
     * @param companyWhiteListVo
     * @return
     */
    @Override
    public int insert(CompanyWhiteListVO companyWhiteListVo) {
        logger.debug("当前新增数据为:" + companyWhiteListVo.toString());
        int num = 0;
        try {
            CompanyWhiteList companyWhiteList = new CompanyWhiteList();
            beanCopy(companyWhiteListVo, companyWhiteList);
            num = companyWhiteListDao.insert(companyWhiteList);
        } catch (Exception e) {
            logger.error("新增数据发生异常!!", e);
            num = -1;
        }
        logger.debug("当前新增数据条数为:" + num);
        return num;
    }

    /**
     * 根据主键删除信息
     *
     * @param companyWhiteListVo
     * @return
     */
    @Override
    public int deleteByPk(CompanyWhiteListVO companyWhiteListVo) {
        logger.debug("当前删除数据条件为:" + companyWhiteListVo);
        int num = 0;
        try {
            CompanyWhiteList companyWhiteList = new CompanyWhiteList();
            beanCopy(companyWhiteListVo, companyWhiteList);
            num = companyWhiteListDao.deleteByPk(companyWhiteList);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + companyWhiteListVo + "删除的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键更新信息
     *
     * @param companyWhiteListVo
     * @return
     */
    @Override
    public int updateByPk(CompanyWhiteListVO companyWhiteListVo) {
        logger.debug("当前修改数据为:" + companyWhiteListVo.toString());
        int num = 0;
        try {
            CompanyWhiteList companyWhiteList = new CompanyWhiteList();
            beanCopy(companyWhiteListVo, companyWhiteList);
            num = companyWhiteListDao.updateByPk(companyWhiteList);
        } catch (Exception e) {
            logger.error("修改数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + companyWhiteListVo + "修改的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键查询详情
     *
     * @param inCompanyWhiteListVo
     * @return
     */
    @Override
    public CompanyWhiteListVO queryByPk(CompanyWhiteListVO inCompanyWhiteListVo) {
        logger.debug("当前查询参数信息为:" + inCompanyWhiteListVo);
        try {
            CompanyWhiteList queryCompanyWhiteList = new CompanyWhiteList();
            beanCopy(inCompanyWhiteListVo, queryCompanyWhiteList);
            CompanyWhiteList queryRslCompanyWhiteList = companyWhiteListDao.queryByPk(queryCompanyWhiteList);
            if (Objects.nonNull(queryRslCompanyWhiteList)) {
                return beanCopy(queryRslCompanyWhiteList, new CompanyWhiteListVO());
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    @Override
    public List<CompanyWhiteListVO> queryCWhiteListByCondition(CompanyWhiteListVO companyWhiteListVO) {
        logger.debug("当前查询参数信息为:" + companyWhiteListVO);
        List<CompanyWhiteListVO> queryRslCompanyWhiteList=null;
        try {
            List<CompanyWhiteList> whiteList = companyWhiteListDao.queryCWhiteListByCondition(companyWhiteListVO);
            queryRslCompanyWhiteList = (List<CompanyWhiteListVO>) beansCopy(whiteList, CompanyWhiteListVO.class);

        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return queryRslCompanyWhiteList;

    }

    /**
     * 查询列表数据
     */
    @Override
    public List<CompanyWhiteListVO> queryAllList(CompanyWhiteListVO inCompanyWhiteListVO) {
        List<CompanyWhiteListVO> list = new ArrayList<CompanyWhiteListVO>();

        if(inCompanyWhiteListVO.getIsQuery()) {//只有点击查询的时候校验
            //校验输入参数是否为空
            if (StringUtils.isBlank(inCompanyWhiteListVO.getCompanyName())
                    && StringUtils.isBlank(inCompanyWhiteListVO.getCompanyType())
                    && StringUtils.isBlank(inCompanyWhiteListVO.getCompanyClass())
                    && StringUtils.isBlank(inCompanyWhiteListVO.getImportEmpNo())
                    && StringUtils.isBlank(inCompanyWhiteListVO.getUpdateName())
                    && (StringUtils.isBlank(inCompanyWhiteListVO.getUpdateStrDt()) || StringUtils.isBlank(inCompanyWhiteListVO.getUpdateEndDt()))
                    && (StringUtils.isBlank(inCompanyWhiteListVO.getImportStrDt()) || StringUtils.isBlank(inCompanyWhiteListVO.getImportEndDt()))
                    && StringUtils.isBlank(inCompanyWhiteListVO.getAppearOrg())
            ) {
                return list;
            }
        }

        CompanyWhiteList companyWhiteList = new CompanyWhiteList();
        beanCopy(inCompanyWhiteListVO, companyWhiteList);
        try {
            List<CompanyWhiteList> companyWhiteLists = companyWhiteListDao.queryAllListByPage(companyWhiteList);
            logger.debug("当前查询本人所属数据信息的结果集数量为:"+ companyWhiteLists.size());

            pageSet(companyWhiteLists, companyWhiteList);

            list = (List<CompanyWhiteListVO>) beansCopy(companyWhiteLists, CompanyWhiteListVO.class);

        } catch (Exception e) {
            logger.error("数据转换出现异常!", e);
        }

        return list;
    }

    @Override
    public int batchDeleteByCompanyId(CompanyWhiteListVO param) {
        logger.debug("当前删除数据条件为:"+ param);
        int num = 0;
        try {
            List<String> compIdList = null;
            if (Objects.nonNull(param) && !StringUtil.isNullorBank(param.getCompanyId())) {
                compIdList = Arrays.asList(param.getCompanyId().split(","));
            }
            num = companyWhiteListDao.batchDeleteByCompanyId(compIdList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int upDateToSetDt(CompanyWhiteListVO companyWhiteListVO){
        logger.debug("当前数据条件为:"+ companyWhiteListVO);
        int num = 0;
        try {
            CompanyWhiteList param = new CompanyWhiteList();
            beanCopy(companyWhiteListVO,param);
            num = companyWhiteListDao.updateSetDt(param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int batchInsertList(List<CompanyWhiteListVO> inserListBatch) {
        logger.debug("当前新增数据条数为:"+ inserListBatch.size());
        int num = 0;
        try {
            num = companyWhiteListDao.batchInsertList(inserListBatch);
        } catch (Exception e) {
            logger.error("新增数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public List<CompanyWhiteListVO> queryConditions(CompanyWhiteListVO companyWhiteListVO) {
        logger.debug("当前查询参数信息为:"+ companyWhiteListVO);
        List<CompanyWhiteListVO> list = null;
        try {
            CompanyWhiteList queryCompanyWhiteList = new CompanyWhiteList();
            beanCopy(companyWhiteListVO, queryCompanyWhiteList);
            List<CompanyWhiteList> resCompanyWhiteList = companyWhiteListDao.queryConditions(queryCompanyWhiteList);
            if (Objects.nonNull(resCompanyWhiteList)&&!resCompanyWhiteList.isEmpty()) {
                List<CompanyWhiteListVO> outCompanyWhiteList = (List<CompanyWhiteListVO>) beansCopy(resCompanyWhiteList, CompanyWhiteListVO.class);
                logger.debug("当前查询结果为:"+ outCompanyWhiteList.toString());
                return outCompanyWhiteList;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    @Override
    public int updateCompany(CompanyWhiteListVO updateCompany){
        logger.debug("当前数据条件为:"+ updateCompany);
        int num = 0;
        try {
            CompanyWhiteList queryCompanyWhiteParam = new CompanyWhiteList();
            beanCopy(updateCompany, queryCompanyWhiteParam);
            num = companyWhiteListDao.updateCompanyWhite(queryCompanyWhiteParam);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int batchDelCompanyWhiteByExcel(List<CompanyWhiteDeleteExcelVO>  companyWhiteDelExcelVO){
        logger.debug("当前删除数据条数为:"+ companyWhiteDelExcelVO.size());
        int num = 0;
        try {
            num = companyWhiteListDao.batchDelCompanyWhiteByExcel(companyWhiteDelExcelVO);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public List<CompanyWhiteListVO> queryCompanyName(CompanyWhiteListVO companyWhiteListVO) throws Exception {
        return (List<CompanyWhiteListVO>) beansCopy(companyWhiteListDao.queryCompanyName(beanCopy(companyWhiteListVO, CompanyWhiteList.class)), CompanyWhiteListVO.class);
    }

    @Override
    public List<CompanyWhiteListVO> queryOverDtList() throws Exception{
        return (List<CompanyWhiteListVO>) beansCopy(companyWhiteListDao.queryOverDtList(),CompanyWhiteListVO.class);
    }

    @Override
    public int deleteByCompName(CompanyWhiteListVO entpNm){
        logger.debug("当前删除数据条数为:"+ entpNm);
        int num = 0;
        try {
            CompanyWhiteList param = new CompanyWhiteList();
            beanCopy(entpNm, param);
            num = companyWhiteListDao.deleteByCompName(param);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int updateByCompName(CompanyWhiteListVO item){
        logger.debug("当前更新数据条数为:"+ item.toString());
        int num = 0;
        try {
            CompanyWhiteList queryCompanyWhiteParam = new CompanyWhiteList();
            beanCopy(item, queryCompanyWhiteParam);
            num = companyWhiteListDao.updateByCompName(queryCompanyWhiteParam);
        } catch (Exception e) {
            logger.error("更新数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    @Override
    public int effectiveDate(){
        int num = 0;
        try {
            num = companyWhiteListDao.effectiveDate();
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
            num = -1;
        }
        return num;
    }
}
